import wordcloud
import numpy as np
from PIL import Image
picture = Image.open("panda.jpg")      # 加载图片文件，返回一个图片对象
mk = np.array(picture)                   # 将图片对象转换成数组
# 创建词云对象
w = wordcloud.WordCloud(font_path='AdobeHeitiStd-Regular.otf', mask=mk,
                             max_words=300, background_color='white')
# 加载词云图用到的文本
file = open(r'xiyouji.txt', encoding='utf-8')
string = file.read()
file.close()
w.generate(string)
# 生成词云图
w.to_file('xiongmao.jpg')
